<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>编码 URL 字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.urldecode.html">« urldecode</a></li>
      <li style="float: right;"><a href="book.v8js.html">V8js »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.url.html">URL 函数</a></li>
    <li>编码 URL 字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.urlencode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">urlencode</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">urlencode</span> &mdash; <span class="dc-title">编码 URL 字符串</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.urlencode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>urlencode</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   此函数便于将字符串编码并将其用于 URL 的请求部分，同时它还便于将变量传递给下一页。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.urlencode-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       要编码的字符串。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.urlencode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回字符串，此字符串中除了 <code class="literal">-_.</code> 
   之外的所有非字母数字字符都将被替换成百分号（<code class="literal">%</code>）后跟两位十六进制数，空格则编码为加号（<code class="literal">+</code>）。此编码与
   WWW 表单 POST 数据的编码方式是一样的，同时与 <code class="literal">application/x-www-form-urlencoded</code>
   的媒体类型编码方式一样。由于历史原因，此编码在将空格编码为加号（+）方面与
   <a href="http://www.faqs.org/rfcs/rfc3986" class="link external">&raquo;&nbsp;RFC3986</a> 编码（参见 <span class="function"><a href="function.rawurlencode.html" class="function">rawurlencode()</a></span>）不同。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.urlencode-examples">
  <h3 class="title">范例</h3> 
  <p class="para">  
   <div class="example" id="example-3850">
    <p><strong>示例 #1 <span class="function"><strong>urlencode()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;a&nbsp;href="mycgi?foo='</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$userinput</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">'"&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3851">
    <p><strong>示例 #2 <span class="function"><strong>urlencode()</strong></span> 与 <span class="function"><a href="function.htmlentities.html" class="function">htmlentities()</a></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$query_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foo='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&amp;bar='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">urlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">'&lt;a&nbsp;href="mycgi?'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$query_string</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'"&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.urlencode-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    注意：小心与 HTML 实体相匹配的变量。像 &amp;amp、&amp;copy 和 &amp;pound 
    都将被浏览器解析，并使用实际实体替代所期待的变量名。这是明显的混乱，W3C
    已经告诫人们好几年了。参考地址：<a href="http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2" class="link external">&raquo;&nbsp;http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2</a>。
   </p>
   <p class="para">
    PHP 通过 arg_separator.ini 指令，支持将参数分割符变成 W3C 
    所建议的分号。不幸的是大多数用户代理并不发送分号分隔符格式的表单数据。较为简单的解决办法是使用
    &amp;amp; 代替 &amp; 作为分隔符。你不需要为此修改 PHP
    的 arg_separator。让它仍为 &amp;，而仅使用 <span class="function"><a href="function.htmlentities.html" class="function">htmlentities()</a></span>
    或 <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> 对你的 URL 进行编码。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.urlencode-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.urldecode.html" class="function" rel="rdfs-seeAlso">urldecode()</a> - 解码已编码的 URL 字符串</span></li>
    <li class="member"><span class="function"><a href="function.htmlentities.html" class="function" rel="rdfs-seeAlso">htmlentities()</a> - 将字符转换为 HTML 转义字符</span></li>
    <li class="member"><span class="function"><a href="function.rawurlencode.html" class="function" rel="rdfs-seeAlso">rawurlencode()</a> - 按照 RFC 3986 对 URL 进行编码</span></li>
    <li class="member"><span class="function"><a href="function.rawurldecode.html" class="function" rel="rdfs-seeAlso">rawurldecode()</a> - 对已编码的 URL 字符串进行解码</span></li>
    <li class="member"><a href="http://www.faqs.org/rfcs/rfc3986" class="link external">&raquo;&nbsp;RFC 3986</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>